import os, sys, time
import requests
from lxml import etree

pwd = os.getcwd()
sys.path.append(pwd)
from proxy import ValidIp



# base_url = 'https://car.autohome.com.cn/config/spec/{}.html'
base_url = 'https://car.autohome.com.cn/config/series/{}.html'
proxies = ValidIp()
for num in range(10000):
    url = base_url.format(num)
    try:
        response = requests.get(url, proxies=proxies)
    except:
        proxies = ValidIp()
        response = requests.get(url, proxies=proxies)
    html = etree.HTML(response.text)
    result = html.xpath('//div[@class="pzbox"]')
    # 保存有参数在售汽车id
    zaishou = open('youcanshu.txt', 'a')
    # 保存没有参数在售汽车id
    tingshou = open('wucanshu.txt', 'a')
    if not result:
        print(num)
        tingshou.write(str(num)+',')
        pass
    else:
        print(result[0], num)
        zaishou.write(str(num)+',')
        pass
    zaishou.close()
    tingshou.close()


    # time.sleep(1)

    